﻿using System;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using SysComm;
using SysOperationLayer.DBHandle;
using IPMS.Common;
using Resources;

public partial class OptCountriesAdd : Page, IRequiresSessionState
{
    protected override void InitializeCulture()
    {
        string strCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();

        if (Session["Culture"] != null)
        {
            strCulture = Session["Culture"].ToString();
        }

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(strCulture);
        base.InitializeCulture();
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorOrNormalUserType())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
        }
        else if (!IsPostBack)
        {
            ((Label)Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Settings + " | " + ISitemap.MaintenanceFiles + " | " + ISitemap.CountryAdd.ToString();
        }
    }
    
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        clearText();
    }
    
    protected void btnSave_Click(object sender, EventArgs e)
    {
        CountriesDB objDB = new CountriesDB();

        if (objDB.Insert(txtCountry.Text.Trim(),
                            txtCountryEng.Text.Trim(),
                            txtCountryChi.Text.Trim(),
                            chkPCTYN.Checked ? 1 : 0,
                            Page.User.Identity.Name.ToString(),
                            HttpContext.Current.Request.UserHostAddress) == 0)
        {
            new LogDB(objDB.GetDB()).writeSucLog(SysUtil.m_functiong_country, "Create New Country Master Success!  Country Code: " + txtCountry.Text.Trim());
            objDB.CloseDB();
            Jscript.AlertAndRedirect("Create New Country Master Success!", "OptCountries.aspx", Page);
        }
        else
        {
            new LogDB(objDB.GetDB()).writeFaileLog(SysUtil.m_functiong_country, "Create New Country Master Failure!  Country Code: " + txtCountry.Text.Trim());
            objDB.CloseDB();
            Jscript.Alert("Create New Country Master Failure!", Page);
            lblError.Text = objDB.ErrorMessage;
        }
    }
    
    protected void clearText()
    {
        txtCountry.Text = string.Empty;
        txtCountryEng.Text = string.Empty;
        txtCountryChi.Text = string.Empty;
        chkPCTYN.Checked = false;
        lblError.Text = "";
    }
    
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("OptCountries.aspx");
    }
}
